﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL.TicketBLL
{
    public class TicketCreationBLL
    {
        public WorkOrderRepository _workOrderRepository = new WorkOrderRepository();
        public ProductRepository _productRepository = new ProductRepository();

        // 添加工单
        public int AddWorkOrder(WorkOrder workOrder)
        {
            // 业务规则验证
            if (workOrder == null)
            {
                throw new ArgumentNullException(nameof(workOrder), "工单对象不能为空");
            }

            if (string.IsNullOrEmpty(workOrder.WorkOrderType))
            {
                throw new ArgumentException("工单类型不能为空", nameof(workOrder));
            }

            if (workOrder.PlannedStartDate > workOrder.PlannedDueDate)
            {
                throw new ArgumentException("计划开始日期不能晚于计划结束日期", nameof(workOrder));
            }

            // 调用数据访问层
            return _workOrderRepository.AddWorkOrder(workOrder);
        }

        // 删除工单
        public int DeleteWorkOrder(int workOrderId)
        {
            // 业务规则验证
            if (workOrderId <= 0)
            {
                throw new ArgumentException("无效的工单ID", nameof(workOrderId));
            }

            // 调用数据访问层
            return _workOrderRepository.DeleteWorkOrder(workOrderId);
        }

        // 更新工单信息
        public int UpdateWorkOrder(WorkOrder workOrder)
        {
            // 业务规则验证
            if (workOrder == null)
            {
                throw new ArgumentNullException(nameof(workOrder), "工单对象不能为空");
            }

            if (workOrder.WorkOrderId <= 0)
            {
                throw new ArgumentException("无效的工单ID", nameof(workOrder));
            }

            if (string.IsNullOrEmpty(workOrder.WorkOrderType))
            {
                throw new ArgumentException("工单类型不能为空", nameof(workOrder));
            }

            //if (workOrder.PlannedStartDate.HasValue && workOrder.PlannedEndDate.HasValue &&
            //    workOrder.PlannedStartDate > workOrder.PlannedEndDate)
            //{
            //    throw new ArgumentException("计划开始日期不能晚于计划结束日期", nameof(workOrder));
            //}

            // 调用数据访问层
            return _workOrderRepository.UpdateWorkOrder(workOrder);
        }

        // 查询所有工单
        public List<WorkOrder> GetAllWorkOrders()
        {
            return _workOrderRepository.GetAllWorkOrders();
        }

        // 查询单个工单
        public WorkOrder GetWorkOrderById(int workOrderId)
        {
            if (workOrderId <= 0)
            {
                throw new ArgumentException("无效的工单ID", nameof(workOrderId));
            }

            return _workOrderRepository.GetWorkOrderById(workOrderId);
        }

        // 获取分页工单列表
        public List<WorkOrder> GetWorkOrdersByPage(int pageIndex, int pageSize, out int totalCount)
        {
            totalCount = 0;
            return _workOrderRepository.GetAllWorkOrders();
        }

        //查询所有产品
        public List<Product> GetAllProducts()
        {
            return _productRepository.GetAllProducts();
        }

        //查询单个产品
        public Product GetProductById(int ProductId)
        {
            if (ProductId <= 0)
            {
                throw new ArgumentException("无效的工单ID", nameof(ProductId));
            }
            return _productRepository.GetProductById(ProductId);
        }

        // 查询所有工单及产品名称
        public List<Dictionary<string, object>> GetAllWorkOrdersWithProduct()
        {
            //return _workOrderRepository.GetAllWorkOrdersWithProduct();
            List<Dictionary<string, object>> test = _workOrderRepository.GetAllWorkOrdersWithProduct();
            // 遍历列表中的每个字典
            foreach (var dict in test)
            {
                Debug.WriteLine("这是字典");
                // 遍历字典中的每个键值对
                foreach (var kvp in dict)
                {
                    Debug.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
                }
                Console.WriteLine(); // 打印一个空行，以便在字典之间分隔
            }
            return test;
        }

        // 带条件的查询方法
        public List<Dictionary<string, object>> GetWorkOrdersWithProductByCondition(
      string workOrderId,
      string productId,
      DateTime? startDate,
      DateTime? endDate)
        {
            // 处理空值转换
            int? workOrderIdInt = null;
            int? productIdInt = null;

            if (!string.IsNullOrEmpty(workOrderId) && int.TryParse(workOrderId, out int woId))
            {
                workOrderIdInt = woId;
            }

            if (!string.IsNullOrEmpty(productId) && int.TryParse(productId, out int pId))
            {
                productIdInt = pId;
            }

            return _workOrderRepository.GetWorkOrdersWithProductByCondition(
                workOrderIdInt,
                productIdInt,
                startDate,
                endDate);
        }

        // 新增取消工单的方法
        public bool CancelWorkOrder(string workOrderId)
        {
            if (string.IsNullOrEmpty(workOrderId))
            {
                throw new ArgumentException("无效的工单ID", nameof(workOrderId));
            }

            int workOrderIdInt;
            if (!int.TryParse(workOrderId, out workOrderIdInt))
            {
                throw new ArgumentException("工单ID必须是有效的整数", nameof(workOrderId));
            }

            return DeleteWorkOrder(workOrderIdInt) > 0;
        }

        //新增更新工单的方法
        public int UpdateTotalProcessId(int workOrderId, int totalProcessId)
        {
            if (workOrderId <= 0)
            {
                throw new ArgumentException("无效的工单ID", nameof(workOrderId));
            }

            return _workOrderRepository.UpdateTotalProcessId(workOrderId, totalProcessId);
        }

        public int UpdateWorkOrderStatus(WorkOrder workOrder)
        {
            return _workOrderRepository.UpdateWorkOrder(workOrder);
        }
    }
}